package net.xumf.service.master.impl;

import com.google.common.collect.Lists;
import net.xumf.dao.master.SystemRoleRepository;
import net.xumf.domain.master.SystemPermission;
import net.xumf.domain.master.SystemRole;
import net.xumf.domain.master.SystemUser;
import net.xumf.service.master.SystemPermissionService;
import net.xumf.service.master.SystemRoleService;
import net.xumf.support.auth.CurrentUser;
import net.xumf.support.common.BaseServiceImpl;
import net.xumf.support.converter.PageConverter;
import net.xumf.support.model.dto.PageDTO;
import net.xumf.support.model.dto.RolePageDTO;
import net.xumf.support.model.vo.PageVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SystemRoleServiceImpl extends BaseServiceImpl<SystemRoleRepository, SystemRole, Integer> implements SystemRoleService {

    @Autowired
    private SystemPermissionService systemPermissionService;

    @Override
    public List<SystemRole> getByRoleIdIn(String roleIdsString) {

        if (StringUtils.isEmpty(roleIdsString)) {
            return Lists.newArrayList();
        }
        List<Integer> roleIds = Arrays.stream(roleIdsString.split(",")).map(Integer::parseInt).collect(Collectors.toList());
        return repository.findByRoleIdIn(roleIds);
    }

    @Override
    public PageVO getByRoleByNameAndStatus(RolePageDTO pageDTO) {
        Pageable pageable = PageConverter.pageVOToPageable(pageDTO);
        Page<SystemRole> rolePage;
        if (null == pageDTO.getStatus()) { // 是否存在status
            rolePage = repository.findByNameContaining(pageDTO.getName(), pageable);
        } else {
            rolePage = repository.findByNameContainingAndStatus(pageDTO.getName(), pageDTO.getStatus(), pageable);
        }
        return PageConverter.pageToPageDTO(rolePage);
    }

    @Override
    public <S extends SystemRole> SystemRole dynamicSave(S s) {
        ArrayList<String> ownPermissionList = Lists.newArrayList(s.getPermissions().split(","));
        List<SystemPermission> permissionList = systemPermissionService.getAllWithModulePath();
        String modulePathStr = permissionList.stream()
                .filter(o -> ownPermissionList.contains(o.getPermissionId().toString()))
                .map(o -> o.getModule().getPath())
                .reduce((a, b) -> a + "," + b).orElse("");
        String modules = Arrays.stream(modulePathStr.split(",")).distinct().reduce((a, b) -> a + "," + b).orElse("");
        s.setModules(modules);
        return super.dynamicSave(s);
    }

    @Override
    public <S extends SystemRole> SystemRole add(S pojo) {
        SystemRole role = new SystemRole();
        BeanUtils.copyProperties(pojo, role);
        SystemUser user = CurrentUser.instance().get();
        role.setCreator(user.getUsername());
        role.setCreatorId(user.getId());
        return super.add(role);
    }
}
